# -*- coding: utf-8 -*-
from django.contrib.auth import authenticate, login, logout
from socialbar.models import Bar
from django.views.generic.simple import direct_to_template
import logging

def verifyStaff(request):
  user = request.user
  if user.is_authenticated() is False or user.is_staff is False: 
    return False
  return True

def redirectNotStaff(request):
  user = request.user
  if user.is_authenticated():
    return direct_to_template(request, 'barManager/index.html',
            {'error' : 'notConnected'})

  if user.is_staff is False:
    return direct_to_template(request, 'barManager/index.html',
        {'error' : 'userIsNotStaff'})
    
def isCurrentBar(bar, request):
  bars = Bar.objects.filter(adminLogin = request.user)
  logging.debug(str(bars[0].id) + ' AND '+ str(bar.id))
  return bars[0].id == bar.id
  
  
  
def barManagerConnect(request):
  user = request.user
  if request.method == 'POST':
      username = request.POST['username']
      password = request.POST['password']
      user = authenticate(username=username, password=password)
      if user is None:
        return direct_to_template(request, 'barManager/index.html',
          {'error' : 'noUser'})
      else:
        login(request, user)

  if user.is_staff is False:
    return direct_to_template(request, 'barManager/index.html',
        {'error' : 'userIsNotStaff'})
  logging.debug('Handler : barManager connected as : ' + user.username)
  return direct_to_template(request, 'barManager/index.html',
        {'success' : 'staffIsConnected'})
  
  
